<?php
/**
 * 控制器基类
 * 
 * @author Dingwu
 */

namespace App\Controllers;

use Frado\Base\Controller;

class BaseController extends Controller {

    /**
     * 是否是静态页面
     * @var bool
     */
    protected $isHtmPage;

    /**
     * html标题
     * @var string
     */
    protected $htmlTitle;

    public function init() {
        parent::init();

        $mchRoute = $this->app['router.match'];
        $this->isHtmPage = $mchRoute['suffix'] ? strpos(config('route.suffix'), $mchRoute['suffix']) : false;

        $this->app['template']->registerFunction('htmlTitle', function () {
            return $this->htmlTitle;
        });
    }

    public function render($file, $data = [], $addData = []) {
        $layout = [];
        /* @var $template \League\Plates\Engine*/
        $template = $this->app['template'];
        if (is_array($addData) && !empty($addData)) {
            $template->addData(array_merge($layout, $addData));
        }
        return $template->render($file, $data);
    }

    public function jsonResp($code, $msg, $other = null) {
        $json = ['stat' => $code, 'msg' => $msg];
        if (is_array($other)) {
            $json = $json + $other;
        }
        return response()->json($json);
    }

}